package fun.hijklmn.magic.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Description
 * @Author fs
 * @Date 2021/6/28 14:50
 * @Version 0.0.1-SNAPSHOT
 **/
public class JsonUtil {

    public static <T> T toBean(String jsonString, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        return JSONObject.toJavaObject(JSONObject.parseObject(jsonString), clazz);
    }

    public static <T> List<T> toArray(String jsonString, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        return JSONArray.parseArray(jsonString, clazz);
    }

    public static String toJsonString(Object object) {
        if (object == null) {
            return null;
        }
        return JSONObject.toJSONString(object);

    }

}
